
from rest_framework import serializers
from .models import *


# 用户序列化器
class UserSer(serializers.ModelSerializer):
    is_state = serializers.ChoiceField(choices=UserModel.STATUS_TYPE, source="get_is_state_display", read_only=True)
    sex = serializers.ChoiceField(choices=UserModel.SEX, source="get_sex_display", read_only=True)

    stations = serializers.PrimaryKeyRelatedField(queryset=StationModel.objects.all())

    stations_name = serializers.SerializerMethodField(read_only=True)

    def get_stations_name(self, obj):
        return obj.stations.name

    class Meta:
        model = UserModel
        fields = '__all__'

# 职级序列化器
class RankSer(serializers.ModelSerializer):
    class Meta:
        model = RankModel
        fields = '__all__'

# 部门序列化器
class SectionSer(serializers.ModelSerializer):
    class Meta:
        model = SectionModel
        fields = '__all__'

# 职位序列化器
class StationSer(serializers.ModelSerializer):
    ranks = serializers.PrimaryKeyRelatedField(queryset=RankModel.objects.all())
    sections = serializers.PrimaryKeyRelatedField(queryset=SectionModel.objects.all())

    ranks_name = serializers.SerializerMethodField()
    sections_name = serializers.SerializerMethodField()

    def get_ranks_name(self, obj):
        return obj.ranks.name

    def get_sections_name(self, obj):
        return obj.sections.name

    class Meta:
        model = StationModel
        fields = '__all__'

# 菜单角色表
class MenuRoleSer(serializers.ModelSerializer):
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    menu = serializers.PrimaryKeyRelatedField(queryset=MenuModel.objects.all())

    role_name = serializers.SerializerMethodField()
    menu_name = serializers.SerializerMethodField()

    def get_role_name(self, obj):
        return obj.role.name
    def get_menu_name(self, obj):
        return obj.menu.name

    class Meta:
        model = MenuRoleModel
        fields = '__all__'

# 菜单序列化器
class MenuSer(serializers.ModelSerializer):

    class Meta:
        model = MenuModel
        fields = '__all__'

# 操作日志序列化器
class HandleSer(serializers.ModelSerializer):
    users = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    users_name = serializers.SerializerMethodField()

    def get_users_name(self, obj):
        return obj.users.name

    class Meta:
        model = HandleModel
        fields = '__all__'

# 邮件序列化器
class MailSer(serializers.ModelSerializer):
    addresser = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    recipient = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())

    addresser_name = serializers.SerializerMethodField()
    recipient_name = serializers.SerializerMethodField()

    def get_addresser_name(self, obj):
        return obj.addresser.name
    def get_recipient_name(self, obj):
        return obj.recipient.name

    class Meta:
        model = MailModel
        fields = '__all__'

# 邮件附件序列化器
class AccessorySer(serializers.ModelSerializer):
    mails = serializers.PrimaryKeyRelatedField(queryset=MailModel.objects.all())

    mails_title = serializers.SerializerMethodField()
    def get_mails_title(self, obj):
        return obj.mails.title

    class Meta:
        model = AccessoryModel
        fields = '__all__'